package com.coollang.smartbra.activity;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.coollang.smartbra.MyAutoLayoutActivity;
import com.coollang.smartbra.R;
import com.coollang.smartbra.presenter.DifficultyActivityPresenter;
import com.coollang.smartbra.utils.ToastUtils;
import com.coollang.smartbra.utils.TranslucentSystemBarUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 训练难度
 */

public class DifficultyActivity extends MyAutoLayoutActivity {

    @BindView(R.id.status_bar)
    View statusBar;
    @BindView(R.id.content)
    RelativeLayout content;
    // ===================== TextView start =================
    @BindView(R.id.rb_item_0)
    RadioButton mRbItem0;
    @BindView(R.id.rb_item_1)
    RadioButton mRbItem1;
    @BindView(R.id.rb_item_2)
    RadioButton mRbItem2;
    @BindView(R.id.rb_item_3)
    RadioButton mRbItem3;
    @BindView(R.id.radio_group)
    RadioGroup mRadioGroup;

    @BindView(R.id.tv_finish)
    TextView mTvFinish;
    // ===================== TextView end =================
    private int mDifficultyPosition = -1;
    private DifficultyActivityPresenter mActivityPresenter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_difficulty);
        ButterKnife.bind(this);
        TranslucentSystemBarUtils.showChenJinZhuangTaiLan(statusBar, this, content);
        initViews();
    }

    private void initViews() {

        // 设置 TextView 的字体
        mRbItem0.setText(setBatteryCount(mRbItem0.getText().toString()));
        mRbItem1.setText(setBatteryCount(mRbItem1.getText().toString()));
        mRbItem2.setText(setBatteryCount(mRbItem2.getText().toString()));
        mRbItem3.setText(setBatteryCount(mRbItem3.getText().toString()));


        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                mTvFinish.setVisibility(View.VISIBLE);
                getCheckDifficuty(checkedId);
            }
        });
    }

    private void getCheckDifficuty(int checkedId) {
        switch (checkedId) {
            case R.id.rb_item_0:
                mDifficultyPosition = 1;
                break;
            case R.id.rb_item_1:
                mDifficultyPosition = 2;
                break;
            case R.id.rb_item_2:
                mDifficultyPosition = 3;
                break;
            case R.id.rb_item_3:
                mDifficultyPosition = 4;
                break;
        }
    }

    private SpannableString setBatteryCount(String text) {
        SpannableString msp = new SpannableString(text);        // 第一行
        msp.setSpan(new RelativeSizeSpan(0.625f), 2, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return msp;
    }


    @Override
    protected void initData() {
        mActivityPresenter = new DifficultyActivityPresenter(this);
    }


    @OnClick({R.id.iv_back, R.id.tv_finish})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_finish:
                mActivityPresenter.setRequest(mDifficultyPosition);
                break;
        }
    }

    public void setGoalSucceed() {
        ToastUtils.showToast("设置成功");
        finish();
    }
}
